#include "Inbox.h"
#include "GestorDeCliente.h"
#include "../common/Conversor.h"
#include <vector>
#include <iostream>
Inbox::Inbox(GestorDeCliente *gestor, Socket *socket) {
	this->gestor = gestor;
	this->socket = socket;
	estado = true;
}

Inbox::~Inbox() {
}

void Inbox::Start(){
	estado = true;
	this->start();
}

void Inbox::Stop() {
	estado = false;
	delete(socket);
	this->stop();
}

void Inbox::ejecutar(){
	do {
		std::string rcv = "";
		try {
			rcv = socket->recibir();
		}catch(int){
			gestor->desconectarDePartida();
			estado = false;
		}
		if ( rcv != ""){
			if (rcv.c_str()[0] == 'G') gestor->notificarPartidas();
			if (rcv.c_str()[0] == 'I') gestor->confirmarInicio();
			std::vector<short> vec = Conversor::deSerializar(rcv);
			if (rcv.c_str()[0] == 'C') gestor->crearPartida(vec[0], vec[1]);
			if (rcv.c_str()[0] == 'U') gestor->unirseAPartida(vec[0]);
			if (rcv.c_str()[0] == 'L') gestor->lanzarHuevo(vec[0], vec[1]);
		}
	} while(estado);
}
